package com.genersoft.iot.vmp.storager.dao;

import com.genersoft.iot.vmp.gb28181.bean.GbStream;
import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface GbStreamMapper {

    @Insert(value = {" <script>  INSERT INTO gb_stream (app, stream, gb_id, name, " +
            "longitude, latitude, stream_type, status) VALUES" +
            "('${app}', '${stream}', '${gbId}', '${name}', " +
            "'${longitude}', '${latitude}', '${streamType}', "+
            "<if test=\"status==true\"> 1 </if> "+
            "<if test=\"status!=true\"> 0 </if> "+
            ") </script>"})
    int add(GbStream gbStream);

    @Update(value = {" <script> UPDATE gb_stream " +
            "SET app=#{app}," +
            "stream=#{stream}," +
            "gb_id=#{gbId}," +
            "name=#{name}," +
            "stream_type=#{streamType}," +
            "longitude=#{longitude}, " +
            "latitude=#{latitude}," +
            "<if test=\"status != null and status==true\"> status=1 , </if> "+
            "<if test=\"status != null and status!=true\"> status=0 ,</if> "+
            "WHERE app=#{app} AND stream=#{stream} AND gb_id=#{gbId}"+
    		" </script>"})
    int update(GbStream gbStream);

    @Delete("DELETE FROM gb_stream WHERE app=#{app} AND stream=#{stream}")
    int del(String app, String stream);

    @Select("SELECT gs.*, pgs.platform_id FROM gb_stream gs LEFT JOIN  platform_gb_stream pgs ON gs.app = pgs.app AND gs.stream = pgs.stream")
    @ResultMap("gbStream")
    @Result(property = "platformId", column = "platform_id")
    List<GbStream> selectAll();

    @Select("SELECT * FROM gb_stream WHERE app=#{app} AND stream=#{stream}")
    @Results(id="gbStream",
    value = { 
       @Result(property = "app", column = "app"), @Result(property = "stream", column = "stream"), 
       @Result(property = "gbId", column = "gb_id"),@Result(property = "name", column = "name"),
       @Result(property = "longitude", column = "longitude"),  @Result(property = "latitude", column = "latitude"), 
       @Result(property = "streamType", column = "stream_type"), @Result(property = "status", column = "status"), 
       })
    StreamProxyItem selectOne(String app, String stream);

    @Select("SELECT gs.*, pgs.platform_id FROM gb_stream gs " +
            "LEFT JOIN platform_gb_stream pgs ON gs.app = pgs.app AND gs.stream = pgs.stream " +
            "WHERE gs.gb_id = '${gbId}' AND pgs.platform_id = '${platformId}'")
    @ResultMap("gbStream")
    @Result(property = "platformId", column = "platform_id")
    GbStream queryStreamInPlatform(String platformId, String gbId);

    @Select("SELECT gs.*, pgs.platform_id FROM gb_stream gs " +
            "LEFT JOIN platform_gb_stream pgs ON gs.app = pgs.app AND gs.stream = pgs.stream " +
            "WHERE pgs.platform_id = '${platformId}'")
    @ResultMap("gbStream")
    @Result(property = "platformId", column = "platform_id")
    List<GbStream> queryGbStreamListInPlatform(String platformId);

    @Update(value = {" <script> UPDATE gb_stream set  " +
            "<if test=\"status != null and status==true\"> status=1 </if> "+
            "<if test=\"status != null and status!=true\"> status=0 </if> "+
            "WHERE app=#{app} AND stream=#{stream}"+
    		" </script>"})
    void setStatus(String app, String stream, boolean status);
}
